package com.me.chat.util.img;

import net.coobird.thumbnailator.Thumbnails;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * 缩略图工具类
 * @Author: qiu
 * @Date: 2021/2/24 10:25
 */
public class TinyImgUtil {

    /**
     * 按固定的比例缩放图片 return 字节数组流
     * @param bytes
     * @return
     */
    public static ByteArrayOutputStream generateScale(byte[] bytes,String extension){
        InputStream byteArr = new ByteArrayInputStream(bytes);
        ByteArrayOutputStream output = null;
        try {
            output = new ByteArrayOutputStream();
            //将MultipartFile内容读出来
            BufferedImage bufferedImage = ImageIO.read(byteArr);
            dealDefault(bufferedImage,extension,output);

        } catch (IOException e) {
            System.out.println("原因: " + e.getMessage());
        } finally {
            if(output != null){
                try {
                    output.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(byteArr != null){
                try {
                    byteArr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return output;
    }


    /**
     * 自定义像素大小生成缩略图
     * @param bytes
     * @param extension
     * @param width
     * @param height
     * @param quality
     * @return
     */
    public static ByteArrayOutputStream generateScale(byte[] bytes,String extension,int width,int height,double quality){
        InputStream byteArr = new ByteArrayInputStream(bytes);
        ByteArrayOutputStream output = null;
        try {
            output = new ByteArrayOutputStream();
            //将MultipartFile内容读出来
            BufferedImage bufferedImage = ImageIO.read(byteArr);
            dealCustom(bufferedImage,extension,output,width,height,quality);

        } catch (IOException e) {
            System.out.println("原因: " + e.getMessage());
        } finally {
            if(output != null){
                try {
                    output.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(byteArr != null){
                try {
                    byteArr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return output;
    }

    /**
     * 自定义百分比缩略图
     * @param bytes
     * @param extension
     * @param scale
     * @param quality
     * @return
     */
    public static ByteArrayOutputStream generateScale(byte[] bytes,String extension,double scale,double quality){
        InputStream byteArr = new ByteArrayInputStream(bytes);
        ByteArrayOutputStream output = null;
        try {
            output = new ByteArrayOutputStream();
            //将MultipartFile内容读出来
            BufferedImage bufferedImage = ImageIO.read(byteArr);
            dealCustom(bufferedImage,extension,output,scale,quality);

        } catch (IOException e) {
            System.out.println("原因: " + e.getMessage());
        } finally {
            if(output != null){
                try {
                    output.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(byteArr != null){
                try {
                    byteArr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return output;
    }

    /**
     * 默认大小的缩小
     * @param bufferedImage
     * @param extension
     * @param outputStream
     * @throws IOException
     */
    private static void dealDefault(BufferedImage bufferedImage,String extension
            ,ByteArrayOutputStream outputStream) throws IOException {
        Thumbnails.of(bufferedImage).
                scale(0.4). // 图片缩放
                outputQuality(0.5). // 图片质量压缩
                outputFormat(extension).
                toOutputStream(outputStream);
    }



    /**
     * 自定义像素缩略图
     * @param bufferedImage
     * @param extension
     * @param outputStream
     * @param width
     * @param height
     * @param quality
     * @throws IOException
     */
    private static void dealCustom(BufferedImage bufferedImage,String extension
            ,ByteArrayOutputStream outputStream,int width,int height,double quality) throws IOException {
        Thumbnails.of(bufferedImage).
                size(width, height). // 图片缩放
                outputQuality(quality). // 图片质量压缩
                outputFormat(extension).
                toOutputStream(outputStream);
    }

    private static void dealCustom(BufferedImage bufferedImage,String extension
            ,ByteArrayOutputStream outputStream,double scale,double quality) throws IOException {
        Thumbnails.of(bufferedImage).
                scale(scale). // 图片缩放
                outputQuality(quality). // 图片质量压缩
                outputFormat(extension).
                toOutputStream(outputStream);
    }

    public static void main(String[] args) throws IOException {
        String jp = "D:/test.jpg";
        String wp = "D:/test.webp";

        Thumbnails.of(jp).
                scale(0.6). // 图片缩放
                outputQuality(0.6). // 图片质量压缩
                toFile(wp);
    }
}
